//
//  listCompany.m
//  iphoneFirstApp
//
//  Created by ye ye on 11-7-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "listCompany.h"
#import "company.h"
#import <libxml/tree.h>

@implementation listCompany

-(NSString *)buildXmlMessage{
	NSString *message=[[NSString alloc] initWithString:@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><root type=\"list\"></root>"];
	return message;
}
-(NSMutableArray *)loadModel:(NSMutableString *)tempString{
	NSError *err=nil;
	NSMutableArray *returnModel=[[NSMutableArray alloc]init];
	tempString=[tempString stringByReplacingOccurrencesOfString:@"xmlns" withString:@"noNSxml"];
	DDXMLDocument *myDocu=[[DDXMLDocument alloc] initWithXMLString:tempString options:0 error:&err];
	if (err){
		@throw [NSException exceptionWithName:@"xmlError" reason:@"xmlString is not a Xml Format" userInfo:nil];
		return nil;
	}
	NSArray *nodes=nil;
	nodes=[myDocu nodesForXPath:@"//root/details/model[@type='company']" error:&err];
	if (err){
		@throw [NSException exceptionWithName:@"xmlError" reason:@"XmlNode读取失败" userInfo:nil];
		return nil;
	}
	for(DDXMLElement *model in nodes){
		company *companyModel=[[company alloc] init];
		[companyModel setId:[[[model attributeForName:@"id"]stringValue] intValue]];
		DDXMLElement *companyInfoNode=[model elementForName:@"companyInfo"];
		[companyModel setCompanyInfo:companyInfoNode.stringValue];
		DDXMLElement *addressNode=[model elementForName:@"address"];
		[companyModel setAddress:addressNode.stringValue];
		DDXMLElement *pointXNode=[model elementForName:@"pointX"];
		[companyModel setPointX:pointXNode.stringValue];
		DDXMLElement *pointYNode=[model elementForName:@"pointY"];
		[companyModel setPointY:pointYNode.stringValue];	
		[returnModel addObject:companyModel];
	}
	return (returnModel);
	
}
@end
